package team.seekers.catering.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.seekers.catering.common.result.R;
import team.seekers.catering.common.result.ResultCode;
import team.seekers.catering.community.entity.dao.Article;
import team.seekers.catering.community.entity.dao.FirstComment;
import team.seekers.catering.community.entity.dao.SecondComment;
import team.seekers.catering.community.entity.dto.add.ArticleAddDTO;
import team.seekers.catering.community.entity.dto.add.FirstCommentAddDTO;
import team.seekers.catering.community.entity.dto.add.SecondCommentAddDTO;
import team.seekers.catering.community.entity.dto.query.ArticleQuery;
import team.seekers.catering.community.entity.dto.query.FirstCommentQuery;
import team.seekers.catering.community.entity.dto.query.SecondCommentQuery;
import team.seekers.catering.community.entity.dto.update.ArticleUpdateDTO;
import team.seekers.catering.community.entity.dto.update.FirstCommentUpdateDTO;
import team.seekers.catering.community.entity.dto.update.SecondCommentUpdateDTO;
import team.seekers.catering.community.entity.vo.info.ArticleInfoVO;
import team.seekers.catering.community.entity.vo.info.FirstCommentInfoVO;
import team.seekers.catering.community.entity.vo.info.SecondCommentInfoVO;
import team.seekers.catering.community.service.FirstCommentService;
import team.seekers.catering.community.service.SecondCommentService;

import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private FirstCommentService firstCommentService;

    @Autowired
    private SecondCommentService secondCommentService;

    /*******************************************一级评论*********************************/

    /**
     * 增加一个一级评论
     */
    @PostMapping("/first/add")
    public R addFirst(@RequestBody FirstCommentAddDTO firstCommentAddDTO){
        String id = firstCommentService.add(firstCommentAddDTO);
        return R.success().data("id",id);
    }

    /**
     * 根据id删除一个一级评论
     */
    @GetMapping("/first/delete/{firstId}")
    public R deleteFirst(@PathVariable("firstId")String id){
        firstCommentService.delete(id);
        return R.success();
    }

    /**
     * 修改一个一级评论
     */
    @PostMapping("/first/update")
    public R updateFirst(@RequestBody FirstCommentUpdateDTO firstCommentUpdateDTO){
        String id = firstCommentService.update(firstCommentUpdateDTO);
        return R.success().data("id",id);
    }

    /**
     * 根据id查询一个一级评论
     */
    @GetMapping("/first/query/{firstId}")
    public R queryFirst(@PathVariable("firstId")String id){
        FirstCommentInfoVO firstCommentInfoVO = firstCommentService.get(id);
        return R.success().data("info", firstCommentInfoVO);
    }

    /**
     * 根据文章或者帖子id分页查询一级评论
     */
    @PostMapping("/first/adminPageFirstComment/{current}/{limit}")
    public R adminPageFirstComment(@PathVariable("current") long current,
                              @PathVariable("limit") long limit,
                              @RequestBody FirstCommentQuery query){
        QueryWrapper<FirstComment> wrapper = new QueryWrapper<>();
        Map<String, Object> map = firstCommentService.adminPageFirstComment(current,limit,wrapper, query.getKeyId());
        if(map == null){
            return R.fail(ResultCode.GET_FAIL);
        }
        return R.success(map);
    }

    /*******************************************二级评论*********************************/

    /**
     * 增加一个二级评论
     */
    @PostMapping("/second/add")
    public R addSecond(@RequestBody SecondCommentAddDTO secondCommentAddDTO){
        String id = secondCommentService.add(secondCommentAddDTO);
        return R.success().data("id",id);
    }

    /**
     * 根据id删除一个二级评论
     */
    @GetMapping("/second/delete/{secondId}")
    public R deleteSecond(@PathVariable("secondId")String id){
        secondCommentService.delete(id);
        return R.success();
    }

    /**
     * 修改一个二级评论
     */
    @PostMapping("/second/update")
    public R updateSecond(@RequestBody SecondCommentUpdateDTO secondCommentUpdateDTO){
        String id = secondCommentService.update(secondCommentUpdateDTO);
        return R.success().data("id",id);
    }

    /**
     * 根据id查询一个二级评论
     */
    @GetMapping("/second/query/{secondId}")
    public R querySecond(@PathVariable("secondId")String id){
        SecondCommentInfoVO secondCommentInfoVO = secondCommentService.get(id);
        return R.success().data("info", secondCommentInfoVO);
    }

    /**
     * 根据一级评论id分页查询二级评论
     */
    @PostMapping("/second/adminPageSecondComment/{current}/{limit}")
    public R adminPageSecondComment(@PathVariable("current") long current,
                                   @PathVariable("limit") long limit,
                                   @RequestBody SecondCommentQuery query){
        QueryWrapper<SecondComment> wrapper = new QueryWrapper<>();
        Map<String, Object> map = secondCommentService.adminPageSecondComment(current,limit,wrapper, query.getKeyId());
        if(map == null){
            return R.fail(ResultCode.GET_FAIL);
        }
        return R.success(map);
    }
}
